package com.pig4cloud.pig.module.controller.platform;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.module.api.entity.platform.PlatformPromotion;
import com.pig4cloud.pig.module.service.platform.IPlatformPromotionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 推广小程序平台Controller
 *
 * @author ruoyi
 * @date 2022-11-03
 */
@Validated
@Api(value = "推广小程序平台控制器", tags = {"推广小程序平台管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/platform/promotion")
public class PlatformPromotionController {
    @Value("${ruoyi.imgUrl}")
    private String imgUrl;
    @Value("${ruoyi.profile}")
    private String uploadPath;
    private final IPlatformPromotionService platformPromotionService;

    /**
     * 查询推广小程序平台列表
     */
    @ApiOperation("查询推广小程序平台列表")
    @PreAuthorize("@pms.hasPermission('module:promotion:list')")
    @GetMapping("/list")
    public TableDataInfo<PlatformPromotion> list(@Validated PlatformPromotion platformPromotion) {
        QueryWrapper<PlatformPromotion> qw = new QueryWrapper<>();

        TableDataInfo<PlatformPromotion> td = PageUtils.buildDataInfo(platformPromotionService.page(PageUtils.buildPage(),qw));
        List<PlatformPromotion> list = td.getData();
        for(PlatformPromotion list1:list){
            if (list1.getLogo()!=null){
                list1.setLogoPath(imgUrl+list1.getLogo());
            }
        }
        return td;
    }

    /**
     * 导出推广小程序平台列表
     */
    @ApiOperation("导出推广小程序平台列表")
    @PreAuthorize("@pms.hasPermission('module:promotion:export')")
    @SysLog("推广小程序平台")
    @GetMapping("/export")
    public List export(@Validated PlatformPromotion platformPromotion) {
        List<PlatformPromotion> list = platformPromotionService.list();
        // ExcelUtil<PlatformPromotion> util = new // ExcelUtil<>(PlatformPromotion.class);
        return  list;// util.exportExcel(list, "推广小程序平台");
    }

    /**
     * 获取推广小程序平台详细信息
     */
    @ApiOperation("获取推广小程序平台详细信息")
    @PreAuthorize("@pms.hasPermission('module:promotion:query')")
    @GetMapping("/{id}")
    public R<PlatformPromotion> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        PlatformPromotion platformPromotion = platformPromotionService.getById(id);
        platformPromotion.setQrCodePath(imgUrl+platformPromotion.getQrCodeUrl());
        return R.ok(platformPromotion);
    }

    /**
     * 新增推广小程序平台
     */
    @ApiOperation("新增推广小程序平台")
    @PreAuthorize("@pms.hasPermission('module:promotion:add')")
    @SysLog("推广小程序平台")

    @PostMapping()
    public R<Void> add(@Valid @RequestBody PlatformPromotion platformPromotion) {
        return R.okByRow(platformPromotionService.save(platformPromotion) ? 1 : 0);
    }

    /**
     * 修改推广小程序平台
     */
    @ApiOperation("修改推广小程序平台")
    @PreAuthorize("@pms.hasPermission('module:promotion:edit')")
    @SysLog("推广小程序平台")

    @PutMapping()
    public R<Void> edit(@Valid @RequestBody PlatformPromotion platformPromotion) {
        return R.okByRow(platformPromotionService.updateById(platformPromotion) ? 1 : 0);
    }

    /**
     * 删除推广小程序平台
     */
    @ApiOperation("删除推广小程序平台")
    @PreAuthorize("@pms.hasPermission('module:promotion:remove')")
    @SysLog("推广小程序平台")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return R.okByRow(platformPromotionService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
/*
    *//**
     * 重置二维码
     *//*
    @SysLog("重置二维码")
    @PostMapping("/resetQrCode")
    @ResponseBody
    @PreAuthorize("@pms.hasPermission('module:promotion:edit')")
    public R<Void> resetQrCode(@RequestBody PlatformPromotion platformPromotion) {
        if(platformPromotion.getId()!=null){
            try{
                platformPromotion = platformPromotionService.getById(platformPromotion.getId());
                if(platformPromotion != null){
                    String qrCode = "";
                    String fileName = "";
                    String content = "";
                    fileName = "platformPromotion_qr_" + platformPromotion.getId() + ".jpg";

                    content = "https://www.sdyuedou.com/?no=" + platformPromotion.getId();

                    int upload_type = FileUploadUtils.TYPE_DEVICE_PHOTOS;
                    System.out.println("content:  "+ content);
                    String img_key = "";
                    img_key = FileUploadUtils.getUploadPrefix(upload_type) + fileName;
                    QRCodeUtilEx.createQrCodeImg(content, platformPromotion.getPlatformName(), null, uploadPath +"/"+img_key, true, fileName, upload_type);
                    qrCode = img_key;
                    platformPromotion.setQrCodeUrl(qrCode);
                }
            }catch (Exception ex){
                ex.printStackTrace();
            }
            if (platformPromotionService.updateById(platformPromotion)) {
                return R.ok();
            }
        }
        return R.failed();
    }*/
}
